﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.form
{
    public partial class formdonhang : Form
    {
        public formdonhang()
        {
            InitializeComponent();
        }
        quanlybansachDataContext db = new quanlybansachDataContext();
        donhang_bll dh = new donhang_bll();
        chitiet_bll ct = new chitiet_bll();
        
        private void btthoat_Click(object sender, EventArgs e)
        {
            formchinh f = new formchinh();
            f.Show();
            this.Close();
        }

        private void btnhaplai_Click(object sender, EventArgs e)
        {
            foreach (var c in groupPanel1.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
            }
        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }
        public void hienthi()
        {
            db = new quanlybansachDataContext();
            dgv.DataSource = from hd in db.donhangs select hd;
            dgv.DataBindings.Clear();
            txtmakh.DataSource = from kh in db.khachhangs select kh;
            txtmakh.DisplayMember = "makh";
            txtmakh.ValueMember = "makh";
            txtsach.DataSource = from sach in db.Saches select sach;
            txtsach.DisplayMember = "ten";
            txtsach.ValueMember = "masach";
        }

        private void formdonhang_Load(object sender, EventArgs e)
        {
            hienthi();
        }

        private void btthem_Click(object sender, EventArgs e)
        {
            donhang obj = new donhang();
            bool check = dh.kiemtradonhang(txtmadh.Text);
            if (check)
            {
                obj.madonhang = txtmadh.Text;
                obj.makh = txtmakh.SelectedValue.ToString();
                obj.ngay = txtngay.Value;
                obj.thanhtien = 0;
                dh.themdonhang(obj);
                lblerror.Text = "Lập hóa đơn thành công";
            }
            else
            {
                MessageBox.Show("Lập hóa đơn thất bại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            db.SubmitChanges();
            hienthi();
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            bool check = dh.kiemtradonhang(txtmadh.Text);
            if (!check)
            {
                dh.xoadonhang(txtmadh.Text);
                lblerror.Text = "Xóa thành công !";
            }
            else
            {
                MessageBox.Show("Không tìm thấy đối tượng cần xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            hienthi();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            chitiet obj1 = new chitiet();
            obj1.madh = txtmadh.Text;
            obj1.masach = txtsach.SelectedValue.ToString();
            ct.themchitiet(obj1);
            lblerror.Text = "Đã thêm 1 sản phẩm vào hóa đơn!";
            dgv.DataSource = from c in db.chitiets where c.madh==txtmadh.Text select c;
        }

        private void btthanhtoan_Click(object sender, EventArgs e)
        {
            db.thanhtoan(txtmadh.Text);
            db.SubmitChanges();
            lblerror.Text = "Đã thanh toán";
            hienthi();
        }
    }
}
